package com.example.demo.lc.str;

import java.util.LinkedHashMap;

/**
* @author wzh
* @date 2021/7/21 2:15 下午
* @Description   lc-3 str
 * https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
*/
public class Lc3 {
    public static void main(String[] args) {
        //String s = "abcabcpwwkewbb";
        //String s = "bbbbb";
        //String s = "pwwkew";
        String s = "dvdf";
        System.out.println(lengthOfLongestSubstring(s));
    }

    /**
    * @author wzh
    * @date 2021/7/21 2:17 下午
    * @Description    滑动窗口
    */
    public static int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        LinkedHashMap<Character,Integer> map = new LinkedHashMap<>();
        int left = 0;
        int max = 0;
        for (int i = 0; i < chars.length; i++) {
            if(map.containsKey(chars[i])){
                left = Math.max(map.get(chars[i])+1,left);
            }
            max = Math.max(i-left+1,max);
            map.put(chars[i],i);
        }
        return max;
    }
}
